/// [Smallest multiple](https://projecteuler.net/problem=5)
fn main() {
    for num in &[10, 20] {
        println!("能被1到{}的所有数均分的最小正数是{}", num, smallest_multiple(*num));
    }
}

/// 解法：统计各个质因数的最大数量
fn smallest_multiple(length: u32) -> u32 {
    let mut prime_number: Vec<(u32, u32)> = vec![(2, 1), (3, 1)];
    for mut num in 4..=length {
        let mut stat = true;
        for (prime, count) in &mut prime_number {
            if 0 == num % (*prime) {
                stat = false;
                let mut n = 0;
                while 0 == num % (*prime) {
                    num /= *prime;
                    n += 1;
                }
                if n > *count {
                    *count = n;
                }
            }
        }
        if stat {
            prime_number.push((num, 1));
        }
    }
    let mut sum = 1;
    for (prime, count) in &prime_number {
        sum *= (*prime).pow(*count);
    }
    sum
}
